#!/usr/bin/env python3
"""Tests for :class:`sievemgr.SieveManager`."""

#
# Copyright 2024  Odin Kroeger
#
# This file is part of SieveManager.
#
# SieveManager is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# SieveManager is distributed in the hope that it will be useful,
# but WITHOUT ALL WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SieveManager. If not, see <https://www.gnu.org/licenses/>.
#

# pylint: disable=missing-class-docstring,missing-function-docstring


#
# Modules
#

import os
import pathlib
import sys
import unittest

from typing import Final

sys.path.append(os.path.realpath(pathlib.Path(__file__).parents[1]))

from sievemgr import SieveManager


#
# Globals
#

SCRIPTNAMES: Final[tuple[tuple[str, bool], ...]] = (
    # Simple tests
    ('', False),
    *[(chr(i), False) for i in range(32)],
    ('\0x7f', False),
    *[(chr(i), False) for i in range(0x80, 0xa0)],
    ('\0x2028', False),
    ('\0x2029', False),
    ('\0xfeff', False),
    ('\n', False),
    ('\r', False),
    ('\0', False),

    # Paradigmatic cases
    ('foo', True),
    ('bar.sieve', True),

    # Edge cases
    (' ', True),

    # Non-ASCII characters
    ('𝚏õȫ', True),
    ('𝕗ōȏ.ⓢℹéṽ𝕖', True)
)


#
# Tests
#

class TestSieveManager(unittest.TestCase):
    def test_validname(self):
        for i, (scriptname, valid) in enumerate(SCRIPTNAMES):
            with self.subTest(i=i, scriptname=scriptname, valid=valid):
                self.assertEqual(SieveManager.validname(scriptname), valid)
                if not valid:
                    try:
                        SieveManager.validname(scriptname, check=True)
                    except ValueError:
                        continue
                    self.fail('expected ValueError')


#
# Boilerplate
#

if __name__ == '__main__':
    unittest.main()
